package com.cskaoyan.service;

import com.cskaoyan.bean.ConfigExpress;
import com.cskaoyan.bean.ConfigOrder;
import com.cskaoyan.bean.ConfigWX;
import com.cskaoyan.mapper.ConfigMapper;
import com.cskaoyan.bean.ConfigMall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author： aurora
 * @data： 2021-05-10 17:54
 */

@Service
public class ConfigServiceImpl implements ConfigService {
    @Autowired
    private ConfigMapper configMapper;


    public static class Pair{
        String from;
        String to;

        public Pair(String from) {
            this.from = from;
            to = from.replace("n_m", "nm");
        }
    }


    /**
     * 获取商城配置
     * @return configMall
     */
    @Override
    public ConfigMall getMallConfig() {

        /*List<String> keyNameList = new ArrayList<>();
        keyNameList.add("cskaoyan_mall_mall_address");
        keyNameList.add("cskaoyan_mall_mall_name");
        keyNameList.add("cskaoyan_mall_mall_phone");
        keyNameList.add("cskaoyan_mall_mall_qq");

        ConfigMall configMallVO = new ConfigMall();
        configMallVO.setCskaoyanmall_mall_address(list.get(0));
        configMallVO.setCskaoyanmall_mall_name(list.get(1));
        configMallVO.setCskaoyanmall_mall_phone(list.get(2));
        configMallVO.setCskaoyanmall_mall_qq(list.get(3));*/

        List<Pair> keyNameList = new ArrayList<>();
        keyNameList.add(new Pair("cskaoyan_mall_mall_address"));
        keyNameList.add(new Pair("cskaoyan_mall_mall_name"));
        keyNameList.add(new Pair("cskaoyan_mall_mall_phone"));
        keyNameList.add(new Pair("cskaoyan_mall_mall_qq"));

        ConfigMall configMall = configMapper.getMallConfig(keyNameList);

        return configMall;
    }

    /**
     * 修改商城配置:数据库中修改更新时间
     * @param configMall
     */
    @Override
    public void updateMallConfig(ConfigMall configMall) {
        Date date = new Date();
        int count = 0;
        count = configMapper.updateMallConfig("cskaoyan_mall_mall_address", configMall.getCskaoyanmall_mall_address(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_mall_name", configMall.getCskaoyanmall_mall_name(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_mall_phone", configMall.getCskaoyanmall_mall_phone(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_mall_qq", configMall.getCskaoyanmall_mall_qq(), date);


    }

    /**
     * 获取运费配置
     * @return
     */
    @Override
    public ConfigExpress getExpressConfig() {
        List<Pair> keyNameList = new ArrayList<>();
        keyNameList.add(new Pair("cskaoyan_mall_express_freight_min"));
        keyNameList.add(new Pair("cskaoyan_mall_express_freight_value"));

        ConfigExpress configExpress = configMapper.getExpressConfig(keyNameList);

        return configExpress;
    }

    /**
     * 修改运费配置:数据库中修改更新时间
     * @param configExpress
     */
    @Override
    public void updateExpressConfig(ConfigExpress configExpress) {
        Date date = new Date();
        int count = 0;
        count = configMapper.updateMallConfig("cskaoyan_mall_express_freight_min", configExpress.getCskaoyanmall_express_freight_min(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_express_freight_value", configExpress.getCskaoyanmall_express_freight_value(), date);
    }

    /**
     * 获取订单配置
     * @return
     */
    @Override
    public ConfigOrder getOrderConfig() {
        List<Pair> keyNameList = new ArrayList<>();
        keyNameList.add(new Pair("cskaoyan_mall_order_comment"));
        keyNameList.add(new Pair("cskaoyan_mall_order_unconfirm"));
        keyNameList.add(new Pair("cskaoyan_mall_order_unpaid"));

        ConfigOrder configOrder = configMapper.getOrderConfig(keyNameList);

        return configOrder;
    }

    /**
     * 修改订单配置:数据库中修改更新时间
     * @param configOrder
     */
    @Override
    public void updateOrderConfig(ConfigOrder configOrder) {
        Date date = new Date();
        int count = 0;
        count = configMapper.updateMallConfig("cskaoyan_mall_order_comment", configOrder.getCskaoyanmall_order_comment(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_order_unconfirm", configOrder.getCskaoyanmall_order_unconfirm(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_order_unpaid", configOrder.getCskaoyanmall_order_unpaid(), date);
    }

    /**
     * 获取小程序配置
     * @return
     */
    @Override
    public ConfigWX getWXConfig() {
        List<Pair> keyNameList = new ArrayList<>();
        keyNameList.add(new Pair("cskaoyan_mall_wx_catlog_goods"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_catlog_list"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_index_brand"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_index_hot"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_index_new"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_index_topic"));
        keyNameList.add(new Pair("cskaoyan_mall_wx_share"));

        ConfigWX configWX = configMapper.getWXConfig(keyNameList);

        return configWX;

    }

    /**
     * 修改小程序配置:数据库中修改更新时间
     * @param configWX
     */
    @Override
    public void updateWXConfig(ConfigWX configWX) {
        Date date = new Date();
        int count = 0;
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_catlog_goods", configWX.getCskaoyanmall_wx_catlog_goods(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_catlog_list", configWX.getCskaoyanmall_wx_catlog_list(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_index_brand", configWX.getCskaoyanmall_wx_index_brand(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_index_hot", configWX.getCskaoyanmall_wx_index_hot(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_index_new", configWX.getCskaoyanmall_wx_index_new(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_index_topic", configWX.getCskaoyanmall_wx_index_topic(), date);
        count = configMapper.updateMallConfig("cskaoyan_mall_wx_share", configWX.getCskaoyanmall_wx_share(), date);

    }

}
